/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.cache.utils.CacheUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.node.ForestNodeMerger;
import org.springblade.core.tool.utils.Func;
import org.springblade.mach.entity.DrawingCategory;
import org.springblade.mach.entity.EquipmentCategory;
import org.springblade.mach.vo.DrawingCategoryVO;
import org.springblade.mach.mapper.DrawingCategoryMapper;
import org.springblade.mach.service.IDrawingCategoryService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 图纸类别表 服务实现类
 *
 * @author zhp
 * @since 2020-12-30
 */
@Service
public class DrawingCategoryServiceImpl extends BaseServiceImpl<DrawingCategoryMapper, DrawingCategory> implements IDrawingCategoryService {

	@Override
	public IPage<DrawingCategoryVO> selectDrawingCategoryPage(IPage<DrawingCategoryVO> page, DrawingCategoryVO drawingCategory) {
		return page.setRecords(baseMapper.selectDrawingCategoryPage(page, drawingCategory));
	}

	@Override
	public List<DrawingCategoryVO> tree() {
		return ForestNodeMerger.merge(baseMapper.tree());
	}

	@Override
	public boolean removeDrawingCategoryVO(String ids) {
		Integer cnt = baseMapper.selectCount(Wrappers.<DrawingCategory>query().lambda().in(DrawingCategory::getParentId, Func.toLongList(ids)));
		if (cnt > 0) {
			throw new ServiceException("请先删除子节点!");
		}
		return removeByIds(Func.toLongList(ids));
	}

	/**
	 * @Author zhp
	 * @Description //TODO
	 * @Date 10:02 2021/11/19
	 * @param id
	 * @return org.springblade.mach.entity.DrawingCategory
	 * 先从缓存中取,找不到在读数据库,并将结果放入缓存
	*/
	public DrawingCategory getById(Long id){
		DrawingCategory drawingCategory = (DrawingCategory) CacheUtil.get("DrawingCategory", "DrawingCategory", id);
		if (drawingCategory == null) {
			drawingCategory = super.getById(id);
			CacheUtil.put("DrawingCategory", "DrawingCategory", id, drawingCategory);
		}
		return drawingCategory;
	}
}
